package com.hqyj.config.shiro;

import com.hqyj.config.UserFilter;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager d){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(d);
        UserFilter userFilter = new UserFilter();
        Map<String,String> map = new HashMap<>();
        Map<String, Filter> map1 = new HashMap<>();
        map1.put("myFilter",userFilter);
        shiroFilterFactoryBean.setFilters(map1);
        map.put("/api/user/emailLogin","anon");
        map.put("/api/user/sendSmsCode","anon");
        map.put("/api/user/sendEmailCode","anon");
        map.put("/api/user/smsLogin","anon");
        map.put("/api/user/userLogin","anon");
        map.put("/**","myFilter");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
        return shiroFilterFactoryBean;
    }

    @Bean(name = "securityManager")
    public DefaultWebSecurityManager securityManager(@Qualifier("UserRealm") UserRealm u){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(u);

        return securityManager;
    }


    @Bean()
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator(){
        DefaultAdvisorAutoProxyCreator proxyCreator = new DefaultAdvisorAutoProxyCreator();
        //扫描advisor，开启注解
        proxyCreator.setProxyTargetClass(true);
        //开启advisor扫描
        proxyCreator.setUsePrefix(true);
        return proxyCreator;
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(@Qualifier("securityManager") DefaultWebSecurityManager d ){
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(d);
        return advisor;
    }

    @Bean(name={"hashedCred"})
    public HashedCredentialsMatcher getMatcher(){
        HashedCredentialsMatcher hsc = new HashedCredentialsMatcher();
        //加密算法
        hsc.setHashAlgorithmName("MD5");
        //加密次数
        hsc.setHashIterations(1024);
        return hsc;
    }

    @Bean(name = {"UserRealm"})
    public UserRealm getRealm(@Qualifier("hashedCred") HashedCredentialsMatcher hs ){
        UserRealm realm = new UserRealm();
        realm.setCredentialsMatcher(hs);
        return realm;
    }

}
